const service = require('../../utils/service.js');
const api = require('../../utils/util.js');
Page({
  data: {
    pay_key: '',
    money: 0,
    offon: true
  },
  //--立即支付
  atOncePay: function() {
    let self = this;
    let share_user_id = wx.getStorageSync('share_user_id');
    api.ajax(service.url('buyOrderPayment'), {
      pay_key: self.data.pay_key,
      pay_id: 3,
      from: 'mini',
      share_user_id: share_user_id
    }, function(res) {
      console.log("小程序支付: " + JSON.stringify(res));
      if (res.code == 0) {
        self.setData({
          offon: false
        })
        let param = res.data;
        param.success = function(res) {
          console.log("调起支付成功：" + JSON.stringify(res));
          if (res.errMsg == 'requestPayment:ok') {
            api.toast("支付成功");
            wx.navigateTo({
              url: '../pay/result/result?money=' + self.data.money
            });
          } else {
            api.toast("支付失败！");
          }
        }
        param.fail = function(res) {
          console.log("调起支付失败！" + JSON.stringify(res));
          api.toast("取消支付");
        }
        wx.requestPayment(param);
      } else {
        this.setData({
          offon: false
        })
        api.toast(res.message);
      }
    });
    setTimeout(() => {
      if (this.data.offon) {
        wx.showModal({
          title: '提示',
          content: '需要前去下载APP',
          confirmText: "前往",
          success: function(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/aApp/aApp',
              })
            }
          }
        })
      }
    }, 5000)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let _money = (+options.money).toFixed(2) || '0.00';
    this.setData({
      money: _money,
      pay_key: options.pay_key
    });
  },
  /**
   * 生命周期回调—监听页面卸载
   */
  onUnload: function() {

  }
})